/*********************************************************************************************************************
**                          闂佽娴烽幊鎾凰囬婊呯幓婵☆垰鍚嬬�氬姊婚崼鐔衡槈閻熸瑱绠撻弻锟犲磼濞戞﹩锟芥粓鏌涜箛鏃戠劷缂侇喗妫冮獮瀣倷閸欏倹顨婇弻锟犲醇濠垫劖鈻堝┑锛勫亾濡炶棄鐣峰Δ鍜佹晢闁逞屽墮椤曪拷?
**                          http://www.guibo.com.cn
**                               闁诲氦顫夐幃鍫曞磿閹惰棄鐓濆┑鍌滎焾缁狀噣鏌ㄩ悢鍓佺煓鐎殿喚鏁婚、娑橆煥閸涱厾鍘哥紓鍌欑閼活垶鎮ラ悡搴樻瀺?
**
**闂備礁鎼崑濠囧箯閿燂拷 濠电偛顕刊鎾箯閿燂拷 闂備礁鎲￠懝鍓х矙閸曨厽顫曢柣褍篓teRecordTask.c
**濠电偠鎻紞鍥箯閿燂拷    闂備礁鍚嬪姗�骞忛幋鐘愁潟 yhai
**闂備礁鎲＄敮妤冪矙閹寸姷纾介柟鎹愵嚙缁秹鏌曟径娑㈡闁糕晛鍊块弻銊モ槈閾忣偄顏�2019.10.18
**濠电儑绲藉ù鍌炲窗濡ゅ懎鏋侀柤娴嬫櫆婵ジ鏌℃径搴㈢《缂佸娼￠弻銊モ槈閾忣偄顏�
**闂備礁鎼崐绋棵洪敐鍛瀻闁靛繈鍨虹�氭岸鎮楀☉娅虫垿锝為敓锟�:
**闂備胶绮〃鎰板箯閿燂拷    闂備礁鎼悧婊堝礈閿曞倸鍑�?.0
**濠电姰鍨奸鎰板箯閿燂拷    婵犵數鍋涢ˇ浠嬫偂婢跺本顫曢柣褍椤�25Q64C
1闂備線娼уΛ鎾箯閿燂拷64M-bit Serial Flash
2闂備線娼уΛ鎾箯閿燂拷8192K-byte -Page -256 bytes per programmable page
3闂備線娼уΛ鏃傜矆娓氾拷椤㈡碍瀵奸弶鎴︽暅闂佽法鍠撻弲顐ゅ垝濮橆優娲敂閸滃啰鍚瑰┑鐐村灦閹碱偊骞忛敓锟�0---0x7fffff
4闂備線娼уΛ鏂款渻閹烘梻鍗氶柟缁㈠枛缁�鍌炴煥閿燂拷?2768濠碉紕鍋戦崐妤呫�傞鐐潟婵犻潧鏌婇幒鏇犵杸婵炴垶鐗戦崑锟�?56bytes闂備焦瀵ч崘濠氬箯閿燂拷
5闂備線娼уΛ鎾箯閿燂拷1page=256bytes      1sector = 16page     1block = 16sector     1chip =128block闂備焦瀵х粙鎴︽嚐椤栫偛鐭楅柟绋垮婵挳鏌熼悙顒�澧繛灞傚�曢埥澶愬箼閸曨剙顏�:
 ____________________________________________________________________________________________________
| Each device has |   Each block has  |  Each sector has    |   Each page has    |                   |
|-----------------|-------------------|---------------------|--------------------|-------------------|
|      8M         |       64          |         4K          |        256         |       bytes       |
|-----------------|-------------------|---------------------|--------------------|-------------------|
|      32K        |       256         |         16          |                    |       pages       |
|-----------------|-------------------|---------------------|--------------------|-------------------|
|      2048       |       16          |                     |                    |       sectoss     |
|-----------------|-------------------|---------------------|--------------------|-------------------|
|      128        |                   |                     |                    |       blocks      |
|-----------------|-------------------|---------------------|--------------------|-------------------|  
  婵犳鍣徊鐣屾崲閹达富鏁冮柣鎾崇岸閺嬪酣鏌熺紒妯虹闁圭晫濞�閺屾稑鐣濈�ｎ偄顏�256bytes闂備焦瀵ч崘濠氬箯閿燂拷
*********************************************************************************************************************/

   
#include "includes.h"


#if DFLASH_GD25_USE

Ex_DFLASH_Analog Ex_DFLASH_AnalogData;    //yhai20190207
uint16_t Ex_DFLASH__RwRecordTime;
uint8_t VolPackageSerial=0,TempPackageSerial=0;
uint8_t Ex_Dflash_ReadBuf[10];
uint16_t Ex_Dflash_Page=0;
uint32_t Ex_Dflash_DebugID;
//static uint32_t  ExDflash_num_CountLast=0;
uint16_t bottv_ms_ExDflash_Fre;

uint8_t  ExDflash_NOR_ChipEraseLockFlag=0;
uint16_t ExDflash_NOR_ChipErase_TimeCnt=10000;
uint8_t ExDflash_NOR_ChipErase_EN_Flag;
uint8_t ExDflash_NOR_DebugID_EN_Flag;
uint16_t LatestRunTimeMin=0;
uint8_t BMS_DatRecordReadSelfLockFlag=0;
uint8_t BMS_DatRecordReadFlag=0;


uint16_t ExDflash_WR_PageOffsetADD=ExDflash_StartPageAdd;
uint16_t ExDflash_RD_PageOffsetADD;
uint8_t  bottv_addr_DatRecodee=1U;      /*闁荤喐绮庢晶妤呭箰閸涘﹥娅犻柣妯款嚙閹瑰爼鏌嶉妷顔筋�旈梻浣规偠閸庤尙绮欓幇鐗堢厒婵犲﹤鎳夐崑鎾斥槈濞咁収浜畷鎴濐吋婢跺﹦顦遍柡澶屽仦婵粙宕㈤鍕厽闁靛鍎遍鈺冪磼椤垵寮�规洩缍侀弫锟�*/

/*
********************************************************************************
**闂備礁鎲￠崹鍏兼叏閵堝姹查柣鏃傚帶鐟欙妇锟藉箍鍎卞▔锕傛倷閻戞ɑ娅栧┑顔界箘椤d DatRecod_init(void)
**闂備礁鎲″Λ鎴﹀箯閿燂拷    闂備胶鍘х壕顓㈡偤閺冨牆鍑�?
**闂備礁鎲￠悷閬嶅箯閿燂拷    闂備浇妗ㄧ粈浣规償濠婂懏顫�?
**闂佸搫顦弲顏堝箯閿燂拷 闂備焦鎮堕崕濠氬箯閿燂拷 闂備胶顭堥敃锕傚Υ鐎ｎ剚顫�?
**濠电姰鍨奸鎰板箯閿燂拷    婵犵數鍋涢¨锟介柟鍑ゆ嫹:
********************************************************************************
*/

uint8_t CalcCheckSum(uint8_t *PointerX,uint8_t CalNumL)
{
	uint8_t i,RVal = 0;
      for(i=0; i< CalNumL;i++)
      {
           RVal  += PointerX[i];
      }
      return RVal;
}



void DatRecod_init(void)
{
   //recover from ex ee 
    uint16_t totalAS=0U;
    uint8_t  eedata[4U][4U];
    uint8_t  temp[2U];
    uint8_t  i,j;
    uint8_t EEPROM_Data[4];
    /*read 4 buffers,start 0x0190U,increase by 4*/
    for(i=0;i<4;i++)
    {

    	exEEP_Read_Bytes(FRAM_DFLASH_ADDR, EEPROM_Data, 4);
        
        for(j=0U;j<4U;j++)
        {
            eedata[i][j]=EEPROM_Data[j];   
        }
        Delay_ms(5U);
    }
    
    /*find the valid data*/
    bottv_addr_DatRecodee=255U;
    
    for(i=0U;i<4U;i++)
    {
        temp[0U]=eedata[i][0U]+eedata[i][1U];
        temp[1U]=0xff-temp[0U];//eedata[i][0U]+eedata[i][1U]+eedata[i][3U];
        
        if((temp[0U]==eedata[i][2U])&&(temp[1U]==eedata[i][3U]))
        {
            totalAS=eedata[i][0U];
            totalAS=totalAS<<8U; 
            totalAS|=eedata[i][1U];
   
            
            if(totalAS!=0xFFFFU)
            {
                ExDflash_WR_PageOffsetADD= totalAS;
                bottv_addr_DatRecodee=i+1;
                if(bottv_addr_DatRecodee>=4U)
                    bottv_addr_DatRecodee=0U;
            }
        }
    }
    
    if(bottv_addr_DatRecodee==255U)
    {
        bottv_addr_DatRecodee=0U; 
        ExDflash_WR_PageOffsetADD=ExDflash_StartPageAdd;
    }
}

/*
********************************************************************************
**闂備礁鎲￠崹鍏兼叏閵堝姹查柣鏃傚帶鐟欙妇锟藉箍鍎卞▔锕傛倷閻戞ɑ娅栧┑顔界箘椤d ExDflashSendEraserSts(void)
**闂備礁鎲″Λ鎴﹀箯閿燂拷    闂備胶鍘х壕顓㈡偤閺冨牆鍑�?
**闂備礁鎲￠悷閬嶅箯閿燂拷    闂備浇妗ㄧ粈浣规償濠婂懏顫�?
**闂佸搫顦弲顏堝箯閿燂拷 闂備焦鎮堕崕濠氬箯閿燂拷 闂備胶顭堥敃锕傚Υ鐎ｎ剚顫�?
**濠电姰鍨奸鎰板箯閿燂拷    婵犵數鍋涢¨锟介柟鍑ゆ嫹:
********************************************************************************
*/
void ExDflashSendEraserSts(void)
{
    stCAN_SWObj    FrameObj;  //闂備礁鎼粔鏉懨洪妶澶婇棷妞ゆ牜鍋涢弸渚�鏌ｅΔ锟介悧鍡欑矈閿曞倹鍋ㄦい鏍ュ�栫�氾拷
    static uint16_t ExDflash_EraserTime;



	FrameObj.ulID.ulVal	=Ex_DlashSendInfID;//IDE,(DIR=TX)DATA Frame,8Data Length code
	FrameObj.ubLen  = 8 ;
	FrameObj.ubData[0] = 0xF1;// 闂備胶鎳撶亸鍛不閹炬枼鏋嶇�广儳灏ex         DFlash Erasering
	if(ExDflash_EraserTime<=59)
	{
		ExDflash_EraserTime++;
		FrameObj.ubData[1] = 0x55;//婵犳鍠楃换鎰緤閽樺鑰挎い蹇撶墕缁犲ジ鏌ㄩ弴鐐诧拷褰掓偟閿燂拷
	}
	else
	{
		ExDflash_EraserTime=0;
		FrameObj.ubData[1] = 0xAA;//闂備胶鎳撶亸鍛不閹炬枼鏋嶉幖杈剧悼绾惧ジ鏌熼幆褏鎽犻悘蹇ユ嫹
		ExDflash_NOR_ChipEraseLockFlag=0;
	}
	FrameObj.ubData[2] = ExDflash_EraserTime;
	FrameObj.ubData[3] = 0;
	FrameObj.ubData[4] = 0;
	FrameObj.ubData[5] = 0;
	FrameObj.ubData[6] = 0;
	FrameObj.ubData[7]   =   (0<<7)|(0<<6)|(0<<5)|(0<<4)|0<<3|ExDflash_NOR_DebugID_EN_Flag<<2|BMS_DatRecordReadSelfLockFlag<<1|ExDflash_NOR_ChipEraseLockFlag;
	Debug_Send_Frame(&FrameObj);


}

/*
********************************************************************************
**闂備礁鎲￠崹鍏兼叏閵堝姹查柣鏃傚帶鐟欙妇锟藉箍鍎卞▔锕傛倷閻戞ɑ娅栧┑顔界箘椤d CanRecExDflashProcess(uint8_t *pData)
**闂備礁鎲″Λ鎴﹀箯閿燂拷    闂備胶鍘х壕顓㈡偤閺冨牆鍑�?
**闂備礁鎲￠悷閬嶅箯閿燂拷    闂備浇妗ㄧ粈浣规償濠婂懏顫�?
**闂佸搫顦弲顏堝箯閿燂拷 闂備焦鎮堕崕濠氬箯閿燂拷 闂備胶顭堥敃锕傚Υ鐎ｎ剚顫�?
**濠电姰鍨奸鎰板箯閿燂拷    婵犵數鍋涢¨锟介柟鍑ゆ嫹:
********************************************************************************
*/
 void CanRecExDflashProcess(uint8_t *pData)
 {
 
    if((pData[0] == 0xA0)&&(pData[1] == 0x0D)&&(pData[2] == 0xB2)&&(pData[3] == 0xC1)
           &&(pData[4] == 0xD0)&&(pData[5] == 0xBE)&&(pData[6] == 0xC6)&&(pData[7] == 0xAC)/*&&(bottv_flg_debug)*/)
    {
        ExDflash_NOR_ChipErase_EN_Flag=1; //A0	0D	B2	C1	D0	BE	C6	AC	闂備胶顭堢换鍫ュ礉鎼粹埗楦跨疀濞戞顔撻梺鎸庢礀閸婂綊鎮甸悜顣濪flash Chip Erase time: 30s typical
    } 
    
    else if ((pData[0] == 0xA0)&&(pData[1] == 0x0E)&&(pData[2] == 0xB6)&&(pData[3] == 0xC1)
           &&(pData[4] == 0x49)&&(pData[5] == 0x44)&&(pData[6] == 0xBA)&&(pData[7] == 0xC5)/*&&(bottv_flg_debug)*/)
    {
        ExDflash_NOR_DebugID_EN_Flag=1;//A0	0E	B6	C1	49	44	BA	C5	闂佽崵濮村ú鈺咁敋瑜戦妵鎰板炊椤掍礁娈熷銈嗙墬缁矂銆傜猾瀛�   (闂備焦鎮堕崕鎶藉磻濞戔懞锟�?xc84017)
    } 
   
    else if((pData[0] == 0xA0)&&(pData[1] == 0x0F)&&(pData[2] == 0xB6)&&(pData[3] == 0xC1)
         &&(pData[4] == 0xCA)&&(pData[5] == 0xFD)&&(pData[6] == 0xBE)&&(pData[7] == 0xDD)/*&&(bottv_flg_debug)*/)
    {
        BMS_DatRecordReadFlag=1;       //A0	0F	B6	C1	CA	FD	BE	DD	闂佽崵濮村ú鈺咁敋瑜戦妵鎰板炊椤掞拷閺嬩線鏌ｅΔ锟介悧鍡欑矈閿曪拷鑿愰柛銉到婢ф彃霉閿燂拷?
    } 

    else
    {
        ;
    }

 }


/*
********************************************************************************
**闂備礁鎲￠崹鍏兼叏閵堝姹查柣鏃傚帶鐟欙妇锟藉箍鍎卞▔锕傛倷閻戞ɑ娅栧┑顔界箘椤d BMS_DatRecordRead(void)
**闂備礁鎲″Λ鎴﹀箯閿燂拷    闂備胶鍘х壕顓㈡偤閺冨牆鍑犻柛鏍ㄥ綀S闂佽崵濮村ú鈺咁敋瑜戦妵鎰板炊椤掞拷閺嬩線鏌ｅΔ锟介悧鍡欑矈閿曞倹鍋ｅù锝堫潐缁惰尙绱撻敓锟�?
**闂備礁鎲￠悷閬嶅箯閿燂拷    闂備浇妗ㄧ粈浣规償濠婂懏顫曢柣銊ョ礉id
**闂佸搫顦弲顏堝箯閿燂拷 闂備焦鎮堕崕濠氬箯閿燂拷 闂備胶顭堥敃锕傚Υ鐎ｎ剚顫曢柣銊ョ礉id
**濠电姰鍨奸鎰板箯閿燂拷    婵犵數鍋涢ˇ浠嬫偂婢跺本顫曞ù鍏兼綑缁犮儲銇勯幇鍓佸埌濞寸厧閰ｉ弻娑樷槈閺嵮冾瀴濠电媴鎷烽柨鐔哄У閺呮悂鏌ㄩ敓锟�?
1闂備線娼уΛ鏂款渻閹烘梻鍗氶柡澶嬪灍閺嬪酣鏌嶉妷銊ョ毢闁荤喐鍔欓弻锝夊閻樺吀绮靛┑鐐存崄閸╂牕顕ラ崟顐僵妞ゆ帪鎷烽悽顖涘浮閹鎷呴崷顓熸啓濡炪倖娲戦崡鍐茬暦閿濆鍐�闁挎梻铏庢导鍥р攽閻愬弶顥戦柛鎾寸箞瀵煡鎮╃紒妯锋寗濠电姴锕ゅΛ顓㈠船閸撲焦鍠愰柣妤�鐗婄粈鍐煛閸℃瑥鏋涙鐐村灴椤㈡岸宕卞▎鎴Х
2闂備線娼уΛ鏂棵归崶顒佸剭闁绘梻鍘ч惌妤呮煛閸垻鈹巗闂備浇妗ㄩ懗鑸垫櫠濡わ拷閻ｅ灚鎷呴悜妯侯�涢梺鍝勵槼濞夋洜绮旈崸妤佺厸闁告劦鍙庨崯鍥╃磼鏉堛劎绠炵�规洘顨婇獮搴ㄦ嚍閵夘垳宸漜ant濠电偛顕慨楣冾敋瑜庨幈銊╂偄閻撳海顦ч梺鍝勭墢閺佹悂鏌ч崒鐐寸厸濞达絽鎼。鑲╋拷鐧告嫹?
3闂備線娼уΛ鏃傜矆娴ｈ櫣绀婂┑鐙�姣坙ash闂備礁鎼悧鍡浰囨导鏉戞辈闁绘梻鍘х粻鍙夈亜閺冨洤袚妞は佸洦鐓欑紒瀣儥濡叉椽鏌涘▎蹇涘弰鐎殿噮鍋婇幃褔宕煎┑鍫涘亰濠电偛鐡ㄧ猾鍌炲川椤旇姤顏熼梻浣圭湽閸斿瞼锟芥凹鍓氶弲鍫曟偐鐠囪尪袝闁瑰吋鐣崝濠囧焵椤掞拷鐎涒晠骞嗛弮鍥╃杸闁靛／鍜佹Х闂備焦鐪归崹鐟扮暦椤掑倻绀婇悗锝庡枛缁�鍫拷骞垮劚濡瑥鈻撻崼鏇熺厵閻犲洠锟借櫕鐏堥梺璇″枟缁挸鐣烽姀銈呭耿婵°倧鎷烽柦鎴濐樀閺屸剝鎷呴崫鍕垫毉閻庤鎸烽悞锕�顭囨繝姘殟闁靛鍎卞В锟�
********************************************************************************
*/
//婵犳鍣徊鐣屾崲閹邦噯鎷烽崹顐�跨�殿噮鍋婇幃褔宕煎┑鍫涘亰闂備礁鎼悧鐐哄箯閿燂拷36濠电偞鍨堕幖鈺傜閿濆鍎婇柡宥庡幖濡ɑ銇勯幘瀵哥畱缂佽妫濋弻鐔碱敇閵婏箑顏� 92濠电偞鍨堕幖鈺傜濠婂啰鏆﹂柣鏂挎啞婵挳鏌熼幑鎰处缂佽妫濋弻鐔碱敇閵婏箑顏� 23+2濠电偞鍨堕幖鈺傜濠靛洨鈹嶆い鏍ㄧ♁閸庣喖鏌ㄩ弬瑁や粧缂佽妫濋弻鐔碱敇閵婏箑顏�  闂備胶顭堟蹇涘箯閿燂拷 36+92*2+23+2=245 bytes
//6闂備礁鎲￠悧鏇㈠箠閹剧粯鍎婇柡宥庡幖濡ɑ銇勯幘璺烘瀾婵炲牆鐖奸弻鐔兼儌閸涘﹤顏�//31闂備礁鎲￠悧鏇㈠箠鎼达紕鏆﹂柣鏂挎啞婵挳鏌熼幆褏鎽犳繛鍫濈埣閺岀喖鎯傞崨濠傤伓//3闂備礁鎲￠悧鏇㈠箠韫囨梻鈹嶆い鏍ㄧ♁閸庣喖鏌ㄩ敓锟�?/1闂備礁鎲￠悧鏇㈠箠鎼淬垺鍙忛柣鎰劋閸婄粯銇勯幘璺烘瀺闁告帒锕獮鏍ㄦ綇妤ｅ啯顎嶉梺鍝勬閸犳牠鐛幋锔芥櫢?/濠电偞鍨堕幖顐﹀箯閻戣姤鐓曢煫鍥ュ劜鐎氾拷41闂備礁鎲￠悧鏇㈠箠韫囨稑姹查柣鏃傚帶缁犳煡鏌ㄩ敓锟�?

void BMS_DatRecordRead(void)
{  
        stCAN_SWObj FrameObj;  //闂備礁鎼粔鏉懨洪妶澶婇棷妞ゆ牜鍋涢弸渚�鏌ｅΔ锟介悧鍡欑矈閿曞倹鍋ㄦい鏍ュ�栫�氾拷
        uint16_t i=0,k=0;
        uint32_t Uint32ReadAdd;
        static uint16_t clccnt1=0, clccnt2=0;
        ExDflash_RD_PageOffsetADD=ExDflash_WR_PageOffsetADD;
        for(i=0;i<=ExDflash_TotalPage_Def;i++) 
        {      
////////////////////////////////////////////////////////////////////////////////////////////////////////                 
            if(clccnt1++>=500)
			{
				clccnt1=0;
				LED_Ctr_OVER();
			}

			if(get_SysIgnSts()==false)
            {
				 break;
			}
////////////////////////////////////////////////////////////////////////////////////////////////////////           
            ExDflash_RD_PageOffsetADD--;//ExDflash_Page_Size;//婵犳鍣徊鐣屾崲閹存繍鐒�?56bit闂傚倸鍊甸崑鎾绘煕椤垵鏋涙い鏃撴嫹
            if(ExDflash_RD_PageOffsetADD<ExDflash_StartPageAdd) ExDflash_RD_PageOffsetADD=ExDflash_EndPageAdd;  //ExDflash_Page_Size;//婵犳鍣徊鐣屾崲閹存繍鐒�?56bit闂傚倸鍊甸崑鎾绘煕椤垵鏋涙い鏃撴嫹
            if(ExDflash_RD_PageOffsetADD>=ExDflash_StartPageAdd)
            {
                Ex_Dflash_Page=ExDflash_RD_PageOffsetADD-ExDflash_StartPageAdd;
            }
            else  Ex_Dflash_Page=ExDflash_RD_PageOffsetADD ;
            
            Uint32ReadAdd=((uint32_t)ExDflash_RD_PageOffsetADD)<<8;

            OS_ENTER_CRITICAL();
            ExDflash_NOR_Read(&Ex_Dflash_ReadBuf[0], Uint32ReadAdd+0, 7);       //8
//            OS_EXIT_CRITICAL();
//            OS_ENTER_CRITICAL();
            ExDflash_NOR_Read(&Ex_Dflash_ReadBuf[7], Uint32ReadAdd+60, 3);       //6
            OS_EXIT_CRITICAL();


            if((Ex_Dflash_ReadBuf[0]==0xff)||(Ex_Dflash_ReadBuf[1]==0xff)||(Ex_Dflash_ReadBuf[2]==0xff)||(Ex_Dflash_ReadBuf[3]==0xff)||(Ex_Dflash_ReadBuf[4]==0xff)||(Ex_Dflash_ReadBuf[5]==0xff) ||(Ex_Dflash_ReadBuf[6]==0xff)
             ||(Ex_Dflash_ReadBuf[0]==0x00)||(Ex_Dflash_ReadBuf[1]==0x00)||(Ex_Dflash_ReadBuf[2]==0x00)/*||(Ex_Dflash_ReadBuf[3]==0x00)||(Ex_Dflash_ReadBuf[4]==0x00)||(Ex_Dflash_ReadBuf[5]==0x00)*/ ||(Ex_Dflash_ReadBuf[6]<0x01)||(Ex_Dflash_ReadBuf[6]>0x04)
			             /*妤犵儑鎷�                                           					 闁哄牞鎷� 							闁哄喛鎷�						                   闁哄喛鎷�								闁告帪鎷�								 缂佸鎷�                                                                  	                    vewmv_enum_workingMode*/
			 ||(0xff!=CalcCheckSum(&Ex_Dflash_ReadBuf[0],8))
			 ||(0xff!=CalcCheckSum(&Ex_Dflash_ReadBuf[8],2))
			   /*鐟滅増鎹侀浼村矗閺嶃劍顦ч梻鍌滅節鐠愶拷0xff闁硅揪鎷�0x00 闁瑰瓨鐗為敓钘夋噺椤ュ懏顨ュ畝鍕櫓閻犲浄鎷� 闁告帗鐟╅敓鏂ゆ嫹闁告垿缂氶姘跺礌閸涱喗娈堕柟璇″枤濞堟垹鎷犵拠鎻掔悼*/
//  			 ||((ExDflash_RD_PageOffsetADD<ExDflash_TotalPage_Def)&&(Ex_Dflash_ReadBuf[0]==RTCTimeArrayLast[0])&&(Ex_Dflash_ReadBuf[1]==RTCTimeArrayLast[1])&&(Ex_Dflash_ReadBuf[2]==RTCTimeArrayLast[2])&&(Ex_Dflash_ReadBuf[3]==RTCTimeArrayLast[3])&&(Ex_Dflash_ReadBuf[4]==RTCTimeArrayLast[4])&&(Ex_Dflash_ReadBuf[5]==RTCTimeArrayLast[5]))
			  )
            {
               continue;
            }

//            OS_ENTER_CRITICAL();
//            for(i=0;i<6;i++)
//            {
//            	RTCTimeArrayLast[i]=Ex_Dflash_ReadBuf[i];
//            }
//            OS_EXIT_CRITICAL();


            if(clccnt2++>=1)
			{
				clccnt2=0;
				LED_Ctr_OVER();
			}


            FrameObj.ulID.ulVal	= Ex_DlashSendInfID ;//0x6A0 ;// ;//IDE,(DIR=TX)DATA Frame,8Data Length code
            FrameObj.ubLen  = 8;
            FrameObj.ubData[0]=0x01;//闂備礁鎲￠悧鏇㈠箠鎼淬們浜�?
            FrameObj.ubData[1]=Ex_Dflash_Page/256;//濠碉紕鍋戦崐婵嗩焽瑜戦妵锟�?
            FrameObj.ubData[2]=Ex_Dflash_Page%256;//濠碉紕鍋戦崐婵嗩焽瑜戦妵锟�?
            for(k=0;k<5;k++) 
            {
                FrameObj.ubData[k+3] = Ex_Dflash_ReadBuf[k];  //闂佺懓鍚嬪娆戞崲閹版澘鍨傛い蹇撶墕閻鏌ㄩ敓锟�?
            }
            Debug_Send_Frame(&FrameObj);
            vTaskDelay(2);


            FrameObj.ulID.ulVal	=  Ex_DlashSendInfID ;//0x6A0 ;// ;//IDE,(DIR=TX)DATA Frame,8Data Length code
            FrameObj.ubLen  = 8; 
            OS_ENTER_CRITICAL();
            ExDflash_NOR_Read(&Ex_Dflash_ReadBuf[0], Uint32ReadAdd+0+5, 7);           //8
            OS_EXIT_CRITICAL();  
            FrameObj.ubData[0]=0x02;
            for(k=0;k<7;k++) 
            {
                FrameObj.ubData[k+1] = Ex_Dflash_ReadBuf[k];  //闂佺懓鍚嬪娆戞崲閹版澘鍨傛い蹇撶墕閻鏌ㄩ敓锟�?
            }
            Debug_Send_Frame(&FrameObj);   //vcan
            vTaskDelay(2);


            FrameObj.ulID.ulVal	=  Ex_DlashSendInfID ;//0x6A0 ;// ;//IDE,(DIR=TX)DATA Frame,8Data Length code
            FrameObj.ubLen  = 8; 
            OS_ENTER_CRITICAL();
            ExDflash_NOR_Read(&Ex_Dflash_ReadBuf[0], Uint32ReadAdd+0+5+7, 7);       //8
            OS_EXIT_CRITICAL();  
            FrameObj.ubData[0]=0x03;
            for(k=0;k<7;k++) 
            {
                FrameObj.ubData[k+1] = Ex_Dflash_ReadBuf[k];  //闂佺懓鍚嬪娆戞崲閹版澘鍨傛い蹇撶墕閻鏌ㄩ敓锟�?
            }
                     
            Debug_Send_Frame(&FrameObj);   //vcan
            vTaskDelay(2);

            FrameObj.ulID.ulVal	=  Ex_DlashSendInfID ;//0x6A0 ;// ;//IDE,(DIR=TX)DATA Frame,8Data Length code
            FrameObj.ubLen  = 8; 
            OS_ENTER_CRITICAL();
            ExDflash_NOR_Read(&Ex_Dflash_ReadBuf[0], Uint32ReadAdd+0+5+7+7, 7);       //8
            OS_EXIT_CRITICAL();  
            FrameObj.ubData[0]=0x04;
            for(k=0;k<7;k++) 
            {
                FrameObj.ubData[k+1] = Ex_Dflash_ReadBuf[k];  //闂佺懓鍚嬪娆戞崲閹版澘鍨傛い蹇撶墕閻鏌ㄩ敓锟�?
            }
                     
            Debug_Send_Frame(&FrameObj);   //vcan
            vTaskDelay(2);

            FrameObj.ulID.ulVal	=  Ex_DlashSendInfID ;//0x6A0 ;// ;//IDE,(DIR=TX)DATA Frame,8Data Length code
            FrameObj.ubLen  = 8; 
            OS_ENTER_CRITICAL();
            ExDflash_NOR_Read(&Ex_Dflash_ReadBuf[0], Uint32ReadAdd+0+5+7+7+7, 7);     //8
            OS_EXIT_CRITICAL();  
            FrameObj.ubData[0]=0x05;
            for(k=0;k<7;k++) 
            {
                FrameObj.ubData[k+1] = Ex_Dflash_ReadBuf[k];  //闂佺懓鍚嬪娆戞崲閹版澘鍨傛い蹇撶墕閻鏌ㄩ敓锟�?
            }     
            Debug_Send_Frame(&FrameObj);   //vcan
            vTaskDelay(2);

            FrameObj.ulID.ulVal	=  Ex_DlashSendInfID ;//0x6A0 ;// ;//IDE,(DIR=TX)DATA Frame,8Data Length code
            FrameObj.ubLen  = 8;
            OS_ENTER_CRITICAL();
            ExDflash_NOR_Read(&Ex_Dflash_ReadBuf[0], Uint32ReadAdd+0+5+7+7+7+7, 7);     //8
            OS_EXIT_CRITICAL();  
            FrameObj.ubData[0]=0x06;
            for(k=0;k<7;k++)
            {
                FrameObj.ubData[k+1] = Ex_Dflash_ReadBuf[k];  //闂佺懓鍚嬪娆戞崲閹版澘鍨傛い蹇撶墕閻鏌ㄩ敓锟�?
            }
            Debug_Send_Frame(&FrameObj);    //vcan
            vTaskDelay(2);
            FrameObj.ulID.ulVal	=  Ex_DlashSendInfID ;//0x6A0 ;// ;//IDE,(DIR=TX)DATA Frame,8Data Length code
            FrameObj.ubLen  = 8;
            OS_ENTER_CRITICAL();
            ExDflash_NOR_Read(&Ex_Dflash_ReadBuf[0], Uint32ReadAdd+0+5+7+7+7+7+7, 7);     //8
            OS_EXIT_CRITICAL();
            FrameObj.ubData[0]=0x07;
            for(k=0;k<7;k++)
            {
                FrameObj.ubData[k+1] = Ex_Dflash_ReadBuf[k];  //闁瑰吋娼欑换鎰板垂椤忓牆鐭楅柨锟�?
            }
            Debug_Send_Frame(&FrameObj);    //vcan
            vTaskDelay(2);
            FrameObj.ulID.ulVal	=  Ex_DlashSendInfID ;//0x6A0 ;// ;//IDE,(DIR=TX)DATA Frame,8Data Length code
            FrameObj.ubLen  = 8;
            OS_ENTER_CRITICAL();
            ExDflash_NOR_Read(&Ex_Dflash_ReadBuf[0], Uint32ReadAdd+0+5+7+7+7+7+7+7, 7);     //8
            OS_EXIT_CRITICAL();
            FrameObj.ubData[0]=0x08;
            for(k=0;k<7;k++)
            {
                FrameObj.ubData[k+1] = Ex_Dflash_ReadBuf[k];  //闁瑰吋娼欑换鎰板垂椤忓牆鐭楅柨锟�?
            }
            Debug_Send_Frame(&FrameObj);    //vcan
            vTaskDelay(2);
            FrameObj.ulID.ulVal	=  Ex_DlashSendInfID ;//0x6A0 ;// ;//IDE,(DIR=TX)DATA Frame,8Data Length code
            FrameObj.ubLen  = 8;
            OS_ENTER_CRITICAL();
            ExDflash_NOR_Read(&Ex_Dflash_ReadBuf[0], Uint32ReadAdd+0+5+7+7+7+7+7+7+7, 7);     //8
            OS_EXIT_CRITICAL();
            FrameObj.ubData[0]=0x09;
            for(k=0;k<7;k++)
            {
                FrameObj.ubData[k+1] = Ex_Dflash_ReadBuf[k];  //闁瑰吋娼欑换鎰板垂椤忓牆鐭楅柨锟�?
            }
            Debug_Send_Frame(&FrameObj);    //vcan
            vTaskDelay(2);
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////    
            FrameObj.ulID.ulVal	=  Ex_DlashSendInfID ;//0x6A0 ;// ;//IDE,(DIR=TX)DATA Frame,8Data Length code
            VolPackageSerial=0;
            while(VolPackageSerial<=17)
            {
                if(VolPackageSerial<17)
                {
                    FrameObj.ubLen  = 8 ; 
                    OS_ENTER_CRITICAL();
                    ExDflash_NOR_Read(&Ex_Dflash_ReadBuf[0], Uint32ReadAdd+64+VolPackageSerial*7, 7);  //7婵炴垶鎼╂禍婊冪暦閻斿憡濯撮柨锟�?
                    OS_EXIT_CRITICAL(); 
                    FrameObj.ubData[0] = 0x10+VolPackageSerial;
                    FrameObj.ubData[1] = Ex_Dflash_ReadBuf[0];    
                    FrameObj.ubData[2] = Ex_Dflash_ReadBuf[1];  
                    FrameObj.ubData[3] = Ex_Dflash_ReadBuf[2];
                    FrameObj.ubData[4] = Ex_Dflash_ReadBuf[3];
                    FrameObj.ubData[5] = Ex_Dflash_ReadBuf[4]; 
                    FrameObj.ubData[6] = Ex_Dflash_ReadBuf[5];
                    FrameObj.ubData[7] = Ex_Dflash_ReadBuf[6];
                }
                else if(VolPackageSerial==17)
                {
                    FrameObj.ubLen  = 8 ; 
                    OS_ENTER_CRITICAL();
                    ExDflash_NOR_Read(&Ex_Dflash_ReadBuf[0], Uint32ReadAdd+64+VolPackageSerial*7, 4);  //4婵炴垶鎼╂禍婊冪暦閻斿憡濯撮柨锟�?
                    OS_EXIT_CRITICAL(); 
                    FrameObj.ubData[0] = 0x10+VolPackageSerial;
                    FrameObj.ubData[1] = Ex_Dflash_ReadBuf[0];    
                    FrameObj.ubData[2] = 0xff;//Ex_Dflash_ReadBuf[1];
                    FrameObj.ubData[3] = 0xff;//Ex_Dflash_ReadBuf[2];
                    FrameObj.ubData[4] = 0xff;//Ex_Dflash_ReadBuf[3];
                    FrameObj.ubData[5] = 0xff;//0xff;
                    FrameObj.ubData[6] = 0xff;//0xff;
                    FrameObj.ubData[7] = 0xff;//0xff;
                }                 
                Debug_Send_Frame(&FrameObj);    //vcan
                vTaskDelay(1);
                VolPackageSerial++;
            }
/////////////////////////////////////////48濠电偞鍨堕幖鈺傜濠靛洨鈹嶆い鏍ㄧ♁閸庣喖鏌ㄩ弴姘辫窗闁搞們鍨介弻娑樷枎濞嗘劕顏�//////////////////////////////////////////////////////////////////////////
            FrameObj.ulID.ulVal	=  Ex_DlashSendInfID ;//0x6A0 ;// ;//IDE,(DIR=TX)DATA Frame,8Data Length code
            TempPackageSerial=0;
            while(TempPackageSerial<=6)
            {
                if(TempPackageSerial<6)
                {
                    FrameObj.ubLen  = 8 ; 
                    OS_ENTER_CRITICAL();
                    ExDflash_NOR_Read(&Ex_Dflash_ReadBuf[0], Uint32ReadAdd+64+120+TempPackageSerial*7, 7);
                    OS_EXIT_CRITICAL(); 
                    FrameObj.ubData[0] = 0xA0+TempPackageSerial;
                    FrameObj.ubData[1] = Ex_Dflash_ReadBuf[0];    
                    FrameObj.ubData[2] = Ex_Dflash_ReadBuf[1];  
                    FrameObj.ubData[3] = Ex_Dflash_ReadBuf[2];
                    FrameObj.ubData[4] = Ex_Dflash_ReadBuf[3];
                    FrameObj.ubData[5] = Ex_Dflash_ReadBuf[4]; 
                    FrameObj.ubData[6] = Ex_Dflash_ReadBuf[5];
                    FrameObj.ubData[7] = Ex_Dflash_ReadBuf[6];
                }
                else if(TempPackageSerial==6)
                {
                    FrameObj.ubLen  = 8 ;
                    OS_ENTER_CRITICAL();
                    ExDflash_NOR_Read(&Ex_Dflash_ReadBuf[0], Uint32ReadAdd+64+120+TempPackageSerial*7, 6);
                    OS_EXIT_CRITICAL(); 
                    FrameObj.ubData[0] = 0xA0+TempPackageSerial;
                    FrameObj.ubData[1] = Ex_Dflash_ReadBuf[0];    
                    FrameObj.ubData[2] = Ex_Dflash_ReadBuf[1];   
                    FrameObj.ubData[3] = Ex_Dflash_ReadBuf[2];
                    FrameObj.ubData[4] = Ex_Dflash_ReadBuf[3];
                    FrameObj.ubData[5] = Ex_Dflash_ReadBuf[4];
                    FrameObj.ubData[6] = Ex_Dflash_ReadBuf[5];
                    FrameObj.ubData[7] = 0xff;
                }                 
               
                Debug_Send_Frame(&FrameObj);//vcan
                vTaskDelay(2);
                TempPackageSerial++;
            }

/////////////////////////////////////////2濠电偞鍨堕幖鈺傜濠婂嫭鍙忛柣鎰劋閸婄粯銇勯幘顖ゆ嫹閾忣偅顏︽繝鐢靛仦閹告挳寮插☉姘炬嫹閻熸壆锛嶉柟椋庡Х閹风娀骞撻幒鏃�鍋�//////////////////////////////////////////////////////////////////////////
        
//            FrameObj.ulID.ulVal	=	Ex_DlashSendInfID ;//0x6A0 ;//IDE,(DIR=TX)DATA Frame,8Data Length code
//            FrameObj.ubLen  = 3 ;
//            OS_ENTER_CRITICAL();
//            ExDflash_NOR_Read(&Ex_Dflash_ReadBuf[0], Uint32ReadAdd+40+144+48, 2);  //2濠电偞鍨堕幖顐﹀箯閿燂拷
//            OS_EXIT_CRITICAL();
//            FrameObj.ubData[0] = 0xE0+1;
//            FrameObj.ubData[1] = Ex_Dflash_ReadBuf[0];
//            FrameObj.ubData[2] = Ex_Dflash_ReadBuf[1];
            //FrameObj.ubData[3] = 0; 
            //FrameObj.ubData[4] = 0;
            //FrameObj.ubData[5] = 0;
            //FrameObj.ubData[6] = 0;
            //FrameObj.ubData[7] = 0;                    
           // Debug_Send_Frame(&FrameObj);    //vcan
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////      
//            vTaskDelay(10);
        }
}

/*
********************************************************************************
**闂備礁鎲￠崹鍏兼叏閵堝姹查柣鏃傚帶鐟欙妇锟藉箍鍎卞▔锕傛倷閻戞ɑ娅栧┑鐐叉閳锋悤eRecordTask
**闂備礁鎲″Λ鎴﹀箯閿燂拷    闂備浇澹堝▍鏇㈠几閼姐倖顫�?
**闂備礁鎲￠悷閬嶅箯閿燂拷    闂備浇妗ㄧ粈浣规償濠婂懏顫�?
**闂佸搫顦弲顏堝箯閿燂拷 闂備焦鎮堕崕濠氬箯閿燂拷 闂備胶顭堥敃锕傚Υ鐎ｎ剚顫�?
**濠电姰鍨奸鎰板箯閿燂拷    婵犵數鍋涢ˇ浠嬫偂婢跺本顫�?
********************************************************************************
*/
void DatRecordProcess(void)
 {
     static uint8_t ID_timeout=0;

	   if(ExDflash_NOR_ChipErase_TimeCnt<10000)ExDflash_NOR_ChipErase_TimeCnt++;
       if((ExDflash_NOR_ChipErase_EN_Flag)&&(ExDflash_NOR_ChipErase_TimeCnt>6000)&&(!BMS_DatRecordReadSelfLockFlag)&&(!ExDflash_NOR_ChipEraseLockFlag))//
       {
            ExDflash_NOR_ChipEraseLockFlag=1; //濠电偞鍨堕幐鎼佹晝閿曞倸鏋侀柛娑橈功閳绘棃鏌ㄩ悢缁橆棄闁宠閰ｉ崺鍕礃閳猴拷缁垱绻濋姀锝呯厫闁煎綊绠栧畷銏ゅ箣閻樺啿顎涢梻鍌︽嫹闁归偊鍠氶幉锕�鈹戦悙鍙夘棏闁告挻绻堥獮妤�鈹戠�ｎ偀鎸冮梺鍖℃嫹?
            ExDflash_NOR_ChipErase_TimeCnt=0;        //闂佸搫顦弲婵嬪磻閻斿吋鍋ㄥ┑鐘宠壘缁犺偐锟藉箍鍎辩�氼喚绮欐繝鍌楁闁规儳纾瓭缂備焦鍎兼禍顒佺濮濆瞼鐤�閹艰揪缍�婢癸拷2闂備礁鎲＄敮鎺懳涘┑瀣?
            ExDflash_WR_PageOffsetADD=ExDflash_StartPageAdd;
//            OS_ENTER_CRITICAL();
            ExDflash_NOR_ChipErase(); //eraser whole chip
//            OS_EXIT_CRITICAL();
            ExDflash_NOR_ChipErase_EN_Flag=0; //闂傚倸鍊稿ù鍕箯閻戣姤鍋￠柡鍥ㄦ皑閸斿秹鏌￠崒娆戠獢鐎规洜鍏樻俊鎼佸Ψ瑜庡鏍⒒閸屾艾顏╅柟鍐茬箻楠炴鈹戠�ｎ偀鎸冮梺闈涚墕閹冲骸袙閹扮増鐓涘〒姘炬嫹妞わ綇濡囩划顓㈠川鐎涙﹩妫冮梺鍖℃嫹?
            if(SaveFlag.sb.DatRecod==0)
            {
                SaveFlag.sb.DatRecod=1U;
            } 
       }

        if((BMS_DatRecordReadFlag)&&(!BMS_DatRecordReadSelfLockFlag)&&(bottv_ms_sysRunTime>3000)) //yhai20170703//濠电偞鍨堕幖顐﹀箯闁垮绠鹃柛鈥崇箚閸嬫捇鎳犻鍌涘暟闂備焦鐪归崹褰掓倶濮橆兙锟芥帡鎳滈棃娑氱獮濠殿喗锕╅崜娆戞暜閹烘鍋ｅù锝呮贡閹藉啯銇勯弴姘祮鐎规洩缍佸浠嬫晜缂侇澀绱樻繝鐢靛仜濡矂宕滃┑瀣柧闁绘绮埛鏇熺節婵犲倹锛嶉柛鐐插閹叉悂鎮ч崼鐔侯槰闂佸搫妫涢崰鏍嵁閹达富鏁嬮柛鈩冪懅椤︻噣姊洪崨濠呭闁哄牜鍓欓—鍐磼閻愯尙顦ч梺瑙勫劶婵倝鏁嶉弨鍍t濠电偛顕慨楣冾敋瑜庨幈銊╂偄閻撳孩鐎梺缁橆殔閻楀棛绮婇敓锟�
        {
            BMS_DatRecordReadSelfLockFlag=1;
            BMS_DatRecordRead();
            BMS_DatRecordReadFlag=0;  //闂傚倸鍊稿ù鍕箯閻戣姤鍋￠柡鍥ㄦ皑閸斿秹鏌￠崒娆戠獢鐎规洜鍏樻俊鎼佸Ψ瑜庡鏍⒒閸屾艾顏╅柟铏崌椤㈡碍瀵奸弶鎴犵厬闂佸搫璇為崘鈺冩И闂備浇妗ㄧ欢姘躲�傞敂鍓х當闁告稑鐡ㄩˉ鍫ユ煥閿燂拷?
            BMS_DatRecordReadSelfLockFlag=0;//闂傚倷鐒﹁ぐ鍐晝閵夆晛妫樺〒姘炬嫹妤犵儑鎷烽梺鐓庮潟閸婃洖鈻撻悩缁樼厸濞达絽鎼。鑲╋拷鐧告嫹?
        }
        
        if(ExDflash_NOR_DebugID_EN_Flag)
        {
            OS_ENTER_CRITICAL();
            Ex_Dflash_DebugID=ExDflash_NOR_ReadID();  //0xc84017
            OS_EXIT_CRITICAL();
            ExDflash_NOR_DebugID_EN_Flag=0;
            ID_timeout=0;
        }

        if(ID_timeout++>0)
        {
        	ID_timeout=3;
        	Ex_Dflash_DebugID=0;
        }




 }
/*
********************************************************************************
**闂備礁鎲￠崹鍏兼叏閵堝姹查柣鏃傚帶鐟欙妇锟藉箍鍎卞▔锕傛倷閻戞ɑ娅栧┑顔界箘椤d DateRecordTask(void *pdata)
**闂備礁鎲″Λ鎴﹀箯閿燂拷    闂備浇澹堝▍鏇㈠几閼姐倖顫�?
**闂備礁鎲￠悷閬嶅箯閿燂拷    闂備浇妗ㄧ粈浣规償濠婂懏顫�?
**闂佸搫顦弲顏堝箯閿燂拷 闂備焦鎮堕崕濠氬箯閿燂拷 闂備胶顭堥敃锕傚Υ鐎ｎ剚顫�?
**濠电姰鍨奸鎰板箯閿燂拷    婵犵數鍋涢ˇ浠嬫偂婢跺本顫�? 50ms
********************************************************************************
*/
void DataRecordStore(void)
{
	uint8_t   i=0;
	static uint16_t  resistance=50000;
	uint32_t  Uint32WriteAdd;
	static uint16_t  ExDflash_Save_WR_Add_Cnt=600;
	static uint8_t  ExDflash_Save_WR_Add_ProtectCnt=0;
	uint32_t Time_DatRecord=600;
	static uint8_t ExDflashflag=0;

    if(ExDflashflag==0)
    {
		ExDflashflag=1;
		ExDflash_Init();
    }

	resistance=bottv_ohm_isoRpRes<bottv_ohm_isoRnRes ? bottv_ohm_isoRpRes:bottv_ohm_isoRnRes;
//////////////////////////////////////////////////package1//////////////////////////////////////////////////////////////////////////////////////////////
	//PageNum   len=2
	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[ 0] =  DEC2BCD(RealTime.year);//
	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[ 1] =  DEC2BCD(RealTime.month);//
	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[ 2] =  DEC2BCD(RealTime.day); //
	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[ 3] =  DEC2BCD(RealTime.hour);
	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[ 4] =  DEC2BCD(RealTime.minute);
///////////////////////////////////////////////////package2/////////////////////////////////////////////////////////////////////////////////////////////
	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[ 5] =  DEC2BCD(RealTime.second);
	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[ 6] =  vewm_enum_workingMode;//vewm_enum_workingMode;
	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[ 7] =  fltm_num_maxFltNo;//cantv_enum_BMS_FaultLevel;//fltm_enum_FltLv;
	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[ 8] =  (uint8_T)(soce_pct_SOC >> 8);
	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[ 9] =  (uint8_T)(soce_pct_SOC >> 0);
	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[10] =  (uint8_T)((uint16_T)((real_T)bottv_v_packVlt * 0.001) >> 8);
	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[11] =  (uint8_T)((uint16_T)((real_T)bottv_v_packVlt * 0.001) >> 0);
///////////////////////////////////////////////////package3/////////////////////////////////////////////////////////////////////////////////////////////
	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[12] =  (uint8_T)((uint16_T)((real_T)bottv_ma_cur * 0.001) >> 8);  //闂備焦鐪归崹褰掓倶濮樿埖鍎撴繝濠傜墕缁犳垿鏌ゆ慨鎰拷鏍汲閳哄倻绠鹃弶鍫㈠亾鐎氾拷    gpio_ma_Curr
	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[13] =  (uint8_T)((uint16_T)((real_T)bottv_ma_cur * 0.001) >> 0);
	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[14] =  (uint8_T)(bottv_mv_p12V >> 8);
	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[15] =  (uint8_T)(bottv_mv_p12V >> 0);
	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[16] =  (uint8_T)(resistance >> 8);
	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[17] =  (uint8_T)(resistance >> 0);
	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[18]=  bottv_no_cVltMax;
	 ///////////////////////////////////////////////////package4/////////////////////////////////////////////////////////////////////////////////////////////
	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[19]=  (uint8_T)(bottv_mv_cVltMax >> 8);
	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[20]=  (uint8_T)(bottv_mv_cVltMax >> 0);
	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[21]=  bottv_no_cVltMin ;
	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[22]=  (uint8_T)(bottv_mv_cVltMin >> 8);
	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[23]=  (uint8_T)(bottv_mv_cVltMin >> 0);
	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[24]=  (uint8_T)(bottv_mv_cellVltAvg >> 8);
	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[25]=  (uint8_T)(bottv_mv_cellVltAvg >> 0);
//////////////////////////////////////////////////package5//////////////////////////////////////////////////////////////////////////////////////////////
	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[26]=  (uint8_T)0;//(cvltv_mv_cVltDif >> 8);  //voldiff
	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[27]=  (uint8_T)0;//(cvltv_mv_cVltDif >> 0);
	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[28]=   bottv_c_cTmpMax;
	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[29]=   bottv_no_cTmpMax;
	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[30]=   bottv_c_cTmpMin;
	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[31]=   bottv_no_cTmpMin;
	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[32]=   bottv_c_cTmpAvg;
//////////////////////////////////////////////////package6//////////////////////////////////////////////////////////////////////////////////////////////
	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[33]=   bottv_k_cpRatio;//ctemv_mv_cTempDif;    //tempdiff
	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[34]=   bottv_mv_SccVolt/20;// (uint8_T)(LatestRunTimeMin >> 8);
	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[35]=   bottv_mv_QccVolt/20;//(uint8_T)(LatestRunTimeMin >> 0);

	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[36]=	 (uint8_T)((uint16_T)((real_T)Rte_mv_VLink_GetVal() * 0.001) >> 8);//Link电压
	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[37]=   (uint8_T)((uint16_T)((real_T)Rte_mv_VLink_GetVal() * 0.001) >> 0);
	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[38]=   (uint8_T)((uint16_T)((real_T)Rte_mv_PtcVol_GetVal() * 0.001) >> 8);//PTC电压
	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[39]=   (uint8_T)((uint16_T)((real_T)Rte_mv_PtcVol_GetVal() * 0.001) >> 0);

	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[40]=  fltm_enum_FaultCode[0];//故障码1
	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[41]=  fltm_enum_FaultCode[1];//故障码2
	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[42]=  fltm_enum_FaultCode[2];//故障码3
	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[43]=  fltm_enum_FaultCode[3];//故障码4
	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[44]=  fltm_enum_FaultCode[4];//故障码5
	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[45]=  fltm_enum_FaultCode[5];//故障码6
	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[46]=  fltm_enum_FltNum;//故障数量

	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[47]=	Rte_flg_MaiRlySts_GetVal();  //主正继电器状态
	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[48]=  Rte_CANTx_PTCRlySts_GetVal();//加热继电器状态
	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[49]=  balc_flg_balanceEn;          //均衡使能
	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[50]=  (uint8_T)(sote_min_SChgRamainingTime >> 8);//充电剩余时间
	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[51]=  (uint8_T)(sote_min_SChgRamainingTime >> 0);
	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[52]=  vewm_enum_SlowChgSts;
	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[53]=  tmsc_enum_HeatSts;

	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[54]=  Rte_enum_HeatTmp1_GetVal();//加热膜1
	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[55]=  Rte_enum_HeatTmp2_GetVal();//加热膜2
	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[56]=  (uint8_T)(bottv_as_soh >> 24);
	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[57]=  (uint8_T)(bottv_as_soh >> 16);
	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[58]=  (uint8_T)(bottv_as_soh >> 8);
	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[59]=  (uint8_T)(bottv_as_soh >> 0);
//////////////////////////////////濞寸姰鍎扮粭鍛村极閻楀牆绁﹀☉鎾崇獘ack1闁稿鐡環eck///////BMSInfCheckDat///////////////////////////////////////////////////////////////////////////////
	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[60]=   0xFF-CalcCheckSum(&Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[0],7 );//BMSInfCheckDat;
	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[61]=   0;//bottv_num_Ltc6815AliveCnt;
	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[62]=   0xFF-0;//bottv_num_Ltc6815AliveCnt;
	Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[63]=   0;
	for(i=0;i<Pdcfg_CellVoltUseNum;i++)         //144濠电偞鍨堕幖鈺傜濠婂啰鏆﹂柣鏂挎啞婵挳鏌ㄩ敓锟�?
	{
		Ex_DFLASH_AnalogData.Ex_DFLASH_VolDat_Array[2*i+0+i/3] = (uint8_T)(bottv_mv_cellVolt[i] >> 8);//充电剩余时间
		Ex_DFLASH_AnalogData.Ex_DFLASH_VolDat_Array[2*i+1+i/3] = (uint8_T)(bottv_mv_cellVolt[i] >> 0);
	}

	for(i=0;i<48;i++)         //48
	{
		Ex_DFLASH_AnalogData.Ex_DFLASH_TempDat_Array[i]   = bottv_c_cellTemp[i]!=0?bottv_c_cellTemp[i]:0xff;
	}
 
//	for(i=0;i<2;i++)         //
//	{
//		Ex_DFLASH_AnalogData.Ex_DFLASH_HTempDat_Array[i]   = 0;//bottc_mv_heatTemp[i];
//	}


	 if(get_SysIgnSts()==true)
	 {

		 if((!ExDflash_NOR_ChipEraseLockFlag)&&(!BMS_DatRecordReadSelfLockFlag)) //闂備線妫跨拃锕傚垂閾忣偅娅犻柡宥冨妿閳绘梻锟藉箍鍎遍幊蹇涚叕椤掑嫭鐓欓柛鏍电磿閻ｅ灚绻涢崨顓㈠弰闁诡喒鏅犻幊婊呭枈婢跺顏堕梺鍖℃嫹?
		{
			Ex_DFLASH__RwRecordTime++;
			if(fltm_num_maxFltNo>=4)         Time_DatRecord = T_DatRecord(1);
			else if(fltm_num_maxFltNo>=3)    Time_DatRecord = T_DatRecord(3);
			else if(fltm_num_maxFltNo>=2)    Time_DatRecord = T_DatRecord(5);
			else if(fltm_num_maxFltNo>=1)    Time_DatRecord = T_DatRecord(10);
			else                           Time_DatRecord = T_DatRecord(30);

			if(Ex_DFLASH__RwRecordTime>=Time_DatRecord)  //婵犳鍠楃换鎰緤娴犲鍋夐柛顐ｆ礀缁秹鏌ｅΟ鎸庣彧婵炲懎锕濠氬川椤斿墽鐓佸┑鈽呮嫹?0S闂佽崵濮抽悞锕�顭垮Ο鑲╃鐎广儱妫涢埢鏃堟煥閻旂粯顥夐柍璇查叄閸┿儵宕ㄩ婊庢Х闁荤喐绮庢晶妤呭箰妞嬪海鍗氶悗闈涙憸濡垳鎲告惔銊ョ柧闁绘绮埛鏇熺節婵犲倸鏆熼柟鐣屽█閺屾盯鏁傞崫鍕瀴濠碉紕鍋涢崐鍧楀极瀹ュ懐鏆嗛柛鎰劤閺佸綊鎮峰鍛暭婵炲弶鐗犻獮濠囧幢濞戞ê鐝橀梺缁樻⒐閸庤偐绮堥敓锟�5S濠电偞鍨堕幖顐﹀箯闁垮绠鹃柛鈥崇箲椤绱掓潏銊х畺缂佸倸绉撮悾婵嬪焵椤掍緡娈介柛銉墮閺嬩線鏌涘☉妯兼憼妞ゆ柨娲弻锟犲礃椤撗冩櫍缂備浇椴哥换鍕箯鐎ｎ喖绾ч悹鎭掑妿鍗忛梻浣告啞缁嬪繘宕濆鍜冭��?S濠电偞鍨堕幖顐﹀箯闁垮绠鹃柛鈥崇箲椤绱掓潏銊х畺缂佸顦板鍕暆閸曨厼绀堥梻浣芥〃缁�渚�骞婂Ο琛℃灁濞村吋娼欑猾宥夋煙椤栨稑顥嬫繛鍫濈埣閺屸剝鎷呴搹鍦槇闁荤媴鎷�?S濠电偞鍨堕幖顐﹀箯闁垮绠鹃柛鐘拷鍐茬盎闁归鍏橀弫锟�?
														   //1闂佽绻愮换鎰崲閹存繍娓婚柛灞剧矋婵ジ鏌℃径搴㈢《缂佸鎷�360濠碉紕鍋戦崐褔骞忛敓锟� 闂備胶顭堟蹇涘箯閿燂拷32768濠碉紕鍋戦崐褔骞忛敓锟�, 闂佽瀛╃粙鎺曟懌閻庢鍟崶褏顓奸梺璺ㄥ枍缁瑩骞冩禒瀣╅柨鏃傜摂濡差垶姊婚崒姘拷鑽ゆ崲濠靛牊瀚�?2768/360=91.2h
			{
				Ex_DFLASH__RwRecordTime=0;


				Uint32WriteAdd=((uint32_t) ExDflash_WR_PageOffsetADD)<<8;
				if(ExDflash_WR_PageOffsetADD % 0x10==0)  //婵犳鍣徊鐣屾崲鐎ｎ喗鐓傛繛鎾跺壋ctor 4K缂傚倷绀侀張顒�顪冮挊澹╂盯宕稿Δ浣稿壄闂佸憡娲﹂崜姘跺箟瑜版帗鈷掗柛鏇″煐鐎氾拷
				{
//					 OS_ENTER_CRITICAL();
					 ExDflash_NOR_SectorErase(Uint32WriteAdd);  // 1page=256bytes      1sector = 16page     1block = 16sector     1chip =128block
//					 OS_EXIT_CRITICAL();
					if(SaveFlag.sb.DatRecod==0)
					{
						SaveFlag.sb.DatRecod=1U;
					}
				}
//				 OS_ENTER_CRITICAL();
				 ExDflash_NOR_Pageprogram(&Ex_DFLASH_AnalogData.Ex_DFLASH_AnalogDatArray[0],Uint32WriteAdd+0, 64U);        //闂佺硶鏅濋崕銈咃耿閻楀牏鈹嶉柍鈺佸暕缁憋拷36婵炴垶鎼幏锟�
//				 OS_EXIT_CRITICAL();

//				OS_ENTER_CRITICAL();
				ExDflash_NOR_Pageprogram(&Ex_DFLASH_AnalogData.Ex_DFLASH_VolDat_Array[0],Uint32WriteAdd+64, 60U);      //闂佸憡顨嗗ú鎴犵礊鐎ｎ喖绀堥柨锟�?2婵炴垶鎼幏锟�
//			    OS_EXIT_CRITICAL();

//				OS_ENTER_CRITICAL();
				ExDflash_NOR_Pageprogram(&Ex_DFLASH_AnalogData.Ex_DFLASH_VolDat_Array[60],Uint32WriteAdd+64+60, 60U);   //闂佸憡顨嗗ú鎴犵礊鐎ｎ喖瑙﹂柨锟�?2婵炴垶鎼幏锟�
//			    OS_EXIT_CRITICAL();

//				OS_ENTER_CRITICAL();
				ExDflash_NOR_Pageprogram(&Ex_DFLASH_AnalogData.Ex_DFLASH_TempDat_Array[0],Uint32WriteAdd+64+60+60, 48U);  //濠电偞鎸撮弲娑氾拷锟�?8婵炴垶鎼幏锟�
//			    OS_EXIT_CRITICAL();

//				OS_ENTER_CRITICAL();
//				ExDflash_NOR_Pageprogram(&Ex_DFLASH_AnalogData.Ex_DFLASH__HTempDat_Array[0],Uint32WriteAdd+39+72+72+48, 2U);   //闂備礁鎲″缁樻叏閻戣棄纾跨憸鐗堝笚閸ゅ绻濇繝鍌氼仹闁告帒锕獮鏍ㄦ綇閻愵剙顏�2濠电偞鍨堕幖顐﹀箯閿燂拷 // 36+92+92+23+2=245
//				OS_EXIT_CRITICAL();

				ExDflash_WR_PageOffsetADD++;// ExDflash_Page_Size;//婵犳鍣徊鐣屾崲閹存繍鐒�?56bit闂傚倸鍊甸崑鎾绘煕椤垵鏋涙い鏃撴嫹
				if(ExDflash_WR_PageOffsetADD>ExDflash_EndPageAdd) ExDflash_WR_PageOffsetADD=ExDflash_StartPageAdd;
			}
		}
   }
   else    //濠电偞鍨堕幐鎼侇敄閸ヮ剙鏋�?
   {
	   //SPI2_SetMode(0);
	    ExDflash_WR_PageOffsetADD &=0x7FFF;    //32767
		if((ExDflash_Save_WR_Add_Cnt++>=600)&&(ExDflash_Save_WR_Add_ProtectCnt<2))    //濠电偞鍨堕幐鎼侇敄閸ヮ剙鏋侀柟闂寸缁狀噣鏌″畵顔煎敪濡ゅ懏鐓涢柛顐墯鐎氳绻濋姀锝呯厫闁活収鍠氱划顓熸媴缁洘鐎婚梺鐐藉劥鐏忔瑧绮绘禒瀣�垫繛鎴ｆ珪鐎氾拷
		{
			ExDflash_Save_WR_Add_Cnt=0;
			ExDflash_Save_WR_Add_ProtectCnt++;
			if(SaveFlag.sb.DatRecod==0)
			{
				SaveFlag.sb.DatRecod=1U;
			}
		}

   }
} 

void DatRecodWrite(void)
{
    uint8_t SysSocDataBuf[2];
    uint8_t sohTemp[2U];
    uint8_t res=0U;
    uint16_t tempSoh;
    uint8_t EEPROM_Data[4];

	tempSoh=ExDflash_WR_PageOffsetADD;

	for(res=0U;res<3U;res++)
	{
		/*write as*/
		SysSocDataBuf[0]=(uint8_t)(tempSoh>>8U);
		SysSocDataBuf[1]=(uint8_t)(tempSoh>>0U);
		exEEP_Write_Bytes(FRAM_DFLASH_ADDR+bottv_addr_DatRecodee*4U, SysSocDataBuf, sizeof(SysSocDataBuf));		//濠电儑绲藉ú锔炬崲閸岀偞鍋�?
		Delay_ms(6);
		exEEP_Read_Bytes(FRAM_DFLASH_ADDR+bottv_addr_DatRecodee*4U, EEPROM_Data, 4);

		if((EEPROM_Data[0]==SysSocDataBuf[0])&&(EEPROM_Data[1]==SysSocDataBuf[1]))
		{
			break;
		}
	}


	sohTemp[0U]=SysSocDataBuf[0U]+SysSocDataBuf[1U];
	sohTemp[1U]=0xff-sohTemp[0U];


	SysSocDataBuf[0U]=sohTemp[0U];
	SysSocDataBuf[1U]=sohTemp[1U];


	if(res<3U)
	{
		for(res=0U;res<3U;res++)
		{
			OS_ENTER_CRITICAL();
			exEEP_Write_Bytes(FRAM_DFLASH_ADDR+bottv_addr_DatRecodee*4U+2U, SysSocDataBuf, sizeof(SysSocDataBuf));		//濠电儑绲藉ú锔炬崲閸岀偞鍋�?
			OS_EXIT_CRITICAL();
			Delay_ms(6);
			OS_ENTER_CRITICAL();
			exEEP_Read_Bytes(FRAM_DFLASH_ADDR+bottv_addr_DatRecodee*4U+2U, EEPROM_Data, 4);
			OS_EXIT_CRITICAL();
			if((EEPROM_Data[0]==SysSocDataBuf[0])&&(EEPROM_Data[1]==SysSocDataBuf[1]))
			{
				bottv_addr_DatRecodee++;
				if(bottv_addr_DatRecodee>=4U)
					bottv_addr_DatRecodee=0U;
				break;
			}
		}
	}
}



/*****************************************************************************
* Date:	2018-12-07
* By:	xzhang
* Func:	闂備浇妗ㄩ懗鑸垫櫠濡わ拷閻ｅ灚鎷呯悰鈥充壕婵炴垶顏鍫濈；鐎广儱妫涢々鐑芥偣閸ャ劌绲绘い顐嫹(5ms濠电偛顕慨楣冾敋瑜庨幈銊╂偄閻撳海顓奸悷婊勫灴閵嗭拷?
*****************************************************************************/
void App_DataRecordTask(void)
{
	TickType_t xNextWakeTime;

	/* Initialise xNextWakeTime - this only needs to be done once.*/
	xNextWakeTime = xTaskGetTickCount();

	for(;;)
	{

		/////////////////////////////////////////////////////////////////////////////////
//			returnValue[13]=(int32_t)uxTaskGetStackHighWaterMark(NULL);
		/////////////////////////////////////////////////////////////////////////////////

		DatRecordProcess();
		DataRecordStore();
		vTaskDelayUntil(&xNextWakeTime, DatRecordTASK_PERIOD / portTICK_PERIOD_MS );
	}
}



#endif



